﻿using Model.Entites.Base;

namespace Utils.Extensions
{
    public class CacheKeyManager<T> where T : ModelBase, new()
    {
        private readonly string _cacheKeyPrefix;
        private readonly List<string> _allCacheKeys = new List<string>();

        public CacheKeyManager()
        {
            _cacheKeyPrefix = typeof(T).Name + "_";
        }

        public void AddCacheKey(string cacheKey)
        {
            _allCacheKeys.Add(cacheKey);
        }

        public void ClearAllRelatedCacheKeys()
        {
            var list = _allCacheKeys.Where(key => key.StartsWith(_cacheKeyPrefix)).ToList();
            if (list?.Count > 0)
            {
                foreach (var key in list)
                {
                    CacheHelper.Remove(key);
                    _allCacheKeys.Remove(key);
                }
            }
        }
    }
}